package org.glut.saltedfish.service;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.glut.saltedfish.domain.ChatRecords;
import org.glut.saltedfish.domain.User;
import org.glut.saltedfish.mapper.ChatRecordsMapper;
import org.glut.saltedfish.utils.MybatisUtil;

import java.util.List;

public class ChatRecordsServiceImpl {
    static Logger logger = Logger.getLogger(UserServiceImpl.class);

    public List<ChatRecords> viewMessageBySI(Integer senderId, Integer receiverId) {
        try (
                SqlSession session = MybatisUtil.getSession()
        ) {
            return session.getMapper(ChatRecordsMapper.class).viewMessageBySI(senderId, receiverId);
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

    public List<ChatRecords> viewMessageByRI(Integer senderId, Integer receiverId) {
        try (
                SqlSession session = MybatisUtil.getSession()
        ) {
            return session.getMapper(ChatRecordsMapper.class).viewMessageByRI(senderId, receiverId);
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

    public Integer unreadMessage(Integer senderId, Integer receiverId) {
        try (
                SqlSession session = MybatisUtil.getSession()
        ) {
            return session.getMapper(ChatRecordsMapper.class).unreadMessage(senderId, receiverId);
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

    public List<ChatRecords> searchObjectBySI(Integer id) {
        try (
                SqlSession session = MybatisUtil.getSession()
        ) {
            return session.getMapper(ChatRecordsMapper.class).searchObjectBySI(id);
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

    public List<ChatRecords> searchObjectByRI(Integer id) {
        try (
                SqlSession session = MybatisUtil.getSession()
        ) {
            return session.getMapper(ChatRecordsMapper.class).searchObjectByRI(id);
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

    public Integer insertChatRecords(ChatRecords record) {
        try (
                SqlSession session = MybatisUtil.getSession()
        ) {
            Integer i = session.getMapper(ChatRecordsMapper.class).insertChatRecords(record);
            session.commit();
            return i;
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }
}